package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@Slf4j
@RequestMapping("/businesses")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    /**
     * 添加商机
     */
    @PostMapping
    public Result add(@RequestBody Business business){
        business.setCreateTime(LocalDateTime.now());
        business.setUpdateTime(LocalDateTime.now());
        log.info("添加商机: {}", business);
        businessService.save( business);
        return Result.success();
    }

    /**
     * 商机列表查询
     */
    @GetMapping
    public Result listBusiness(BusinessDto businessDto){
       PageResult<Business> lists = businessService.listBusiness(businessDto);
       return Result.success(lists);
    }

    /**
     * 分配商机
     */
    @PutMapping("/assign/{businessId}/{userId}")
    public Result assignBusiness(@PathVariable Integer businessId,@PathVariable Integer userId){
        Business business = new Business();
        business.setId(businessId);
        business.setUserId(userId);
        business.setStatus(2);
        business.setUpdateTime(LocalDateTime.now());
        businessService.updateById(business);
        return Result.success();
    }

    /**
     * 根据ID查询商机
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
       Business business = businessService.getBusinessById(id);
       return Result.success(business);
    }

    /**
     * 跟进商机
     */
    @PutMapping
    public Result trackBusiness(@RequestBody Business  business){
        businessService.trackBusiness(business);


        return Result.success();
    }

    /**
     * 踢回公海
     */
    @PutMapping("back/{id}")
    public Result backToPool(@PathVariable Integer id){
        businessService.backToPool(id);
        return Result.success();
    }

    /**
     * 转客户处理
     */
    @PostMapping("/toCustomer/{id}")
    public Result toCustomer(@PathVariable Integer id){
        businessService.toCustomer(id);
        return Result.success();
    }

    /**
     * 公海池列表
     */
    @GetMapping("/pool")
    public Result listPoolBusiness(BusinessDto businessDto){
        PageResult<Business> pageResult = businessService.listPoolBusiness(businessDto);
        return Result.success(pageResult);
    }
}
